package com.zkingsoft.picbroswer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.edmodo.cropper.CropImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutionException;

public class CropActivity extends AppCompatActivity
{

    ImageView ivBack;
    TextView tvSelectedOk;
    com.edmodo.cropper.CropImageView cropImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //设置沉浸式样式
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_crop);

        File f = (File) getIntent().getSerializableExtra("raw");
        if(f != null && f.exists())
        {
            init(f);
        }else
        {
            finish();
        }
    }

    private void init(File f)
    {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvSelectedOk = (TextView) findViewById(R.id.tv_selected_ok);
        cropImageView = (CropImageView) findViewById(R.id.cropImageView);
        cropImageView.setFixedAspectRatio(false);
        //有网格
        cropImageView.setGuidelines(2);
        Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
        if(bmp != null)
        {
            cropImageView.setImageBitmap(bmp);
        }
        ivBack.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
        tvSelectedOk.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Bitmap bmp = cropImageView.getCroppedImage();
                if(bmp != null)
                {
                    try
                    {
                        String path = saveImage(bmp, getAppCacheDir(CropActivity.this)+"/crop.png");
                        Intent intent = new Intent(CropActivity.this, AlbumActivity.class);
                        intent.putExtra("cropimg", path);
                        setResult(ActivityCode.SELECT_OK_RESULT, intent);
                        finish();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    /**
     * 位图保存到本地
     *
     * @param bmp 位图对象
     * @return 图像保存后的绝对路径包含图片的完整名称
     * @throws Exception
     */
    private String saveImage(Bitmap bmp, String imgPath) throws Exception
    {
        File file = new File(imgPath);
        FileOutputStream oStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, oStream); // 100是照片质量，0-100，越大越好
        oStream.flush();
        oStream.close();
        return file.getAbsolutePath();
    }

    /**
     * 获取缓存路径
     *
     * @param context
     * @return
     */
    public String getAppCacheDir(Context context)
    {
        String cachePath = null;
        //获取外部缓存目录
        //准备就绪就返回，否则返回空
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable())
        {
            return context.getExternalCacheDir().getPath();
        } else
        {
            //返回内部缓存目录
            return context.getCacheDir().getPath();
        }
    }

}
